#version 330 core
layout(location=0) in vec3 aPos;
layout(location=1) in vec3 aNormal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

in vec3 Normal;
in vec3 FragPos;
uniform vec3 lightColor;
uniform vec3 lightPos;
uniform vec3 viewPos;
out vec3 LightingColor;

void main(){
    vec3 FragPos=vec3(model*vec4(aPos,1.0f));         //世界坐标系
    vec3 Normal=mat3(transpose(inverse(model)))*aNormal;       //使用法线矩阵
    gl_Position=projection*view*vec4(FragPos,1.0f);

    //ambient
    float ambientStrength=0.1;
    vec3 ambient=ambientStrength*lightColor;

    vec3 norm=normalize(Normal);
    vec3 lightDir=normalize(lightPos-FragPos);

    float diff=max(dot(norm,lightDir),0.0);
    vec3 diffuse=diff*lightColor;

    float specularStrength=0.5;
    vec3 viewView=normalize(viewPos-FragPos);

    vec3 reflectDir=reflect(-lightDir,norm);
    float spec=pow(max(dot(viewView,reflectDir),0.0),32);
    vec3 specular=specularStrength*spec*lightColor;

    LightingColor=ambient+diffuse+specular;

}
